package com.orange.res;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import com.orange.audio.sound.ISound;
import com.orange.audio.sound.ISoundFactory;
import com.orange.audio.sound.ISoundManager;
import com.orange.util.file.IFileManage;

public class SoundRes {


	// ===========================================================
	// 变量
	// ===========================================================
	private final IFileManage mFileManage;
	private final ISoundManager mSoundManager;
	private final ISoundFactory mSoundFactory;
	
	private final Map<String, ISound> mSoundMap = new HashMap<String, ISound>();

	// ===========================================================
	// 构造
	// ===========================================================

	public SoundRes(IFileManage pFileManage, ISoundManager pSoundManager, ISoundFactory pSoundFactory) {
		// SoundFactory.setAssetBasePath("mfx/");
		this.mFileManage = pFileManage;
		this.mSoundManager = pSoundManager;
		this.mSoundFactory = pSoundFactory;
	}

	public void loadSoundFromAssetsFile(String pKey, String pSoundFileName) {
		try {
			ISound sound = mSoundFactory.createSoundFromAsset(this.mSoundManager, this.mFileManage, pSoundFileName);
			this.mSoundMap.put(pKey, sound);
		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void playSoundFromMap(String pKey) {
		ISound sound = this.mSoundMap.get(pKey);
		if (sound != null) {
			sound.play();
		}
	}

	public ISound getSoundFromMap(String pKey) {
		return this.mSoundMap.get(pKey);
	}

	public void setMasterVolume(float pMasterVolume) {
		this.mSoundManager.setMasterVolume(pMasterVolume);
	}
	
	public float getMasterVolume(){
		return this.mSoundManager.getMasterVolume();
	}


	// ===========================================================
	// 静态
	// ===========================================================
	
	public static void loadSoundFromAssets(String pKey, String pSoundFileName) {
		ResManager.getInstance().getSoundRes().loadSoundFromAssetsFile(pKey, pSoundFileName);
	}
	
	public static void loadSoundFromAssets(String pKey) {
		ResManager.getInstance().getSoundRes().loadSoundFromAssetsFile(pKey, pKey);
	}

	public static void playSound(String pKey) {
		ResManager.getInstance().getSoundRes().playSoundFromMap(pKey);
	}

	public static ISound getSound(String pKey) {
		return ResManager.getInstance().getSoundRes().getSoundFromMap(pKey);
	}
	
	public static void setVolume(float pMasterVolume) {
		ResManager.getInstance().getSoundRes().setMasterVolume(pMasterVolume);
	}
	
	public static float getVolume(){
		return ResManager.getInstance().getSoundRes().getMasterVolume();
	}

	public static void onSound(float pMasterVolume) {
		ResManager.getInstance().getSoundRes().setMasterVolume(pMasterVolume);
	}

	public static void offSound() {
		ResManager.getInstance().getSoundRes().setMasterVolume(0.0f);
	}


}
